{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_mutation:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mutation"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_mutation_pm:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Polynomial Mutation (PM)\n",
    "\n",
    "Details about the mutation can be found in <cite data-cite=\"sbx\"></cite>. This mutation follows the same probability distribution as the simulated binary crossover."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from pymoo.core.population import Population\n",
    "from pymoo.core.problem import Problem\n",
    "from pymoo.operators.mutation.pm import PolynomialMutation\n",
    "\n",
    "\n",
    "def show(eta_mut):\n",
    "    problem = Problem(n_var=1, xl=0.0, xu=1.0)\n",
    "\n",
    "    X = np.full((5000, 1), 0.5)\n",
    "    pop = Population.new(X=X)\n",
    "\n",
    "    mutation = PolynomialMutation(prob=1.0, eta=eta_mut)\n",
    "\n",
    "    off = mutation(problem, pop)\n",
    "    Xp = off.get(\"X\")\n",
    "\n",
    "    plt.hist(Xp, range=(0.0, 1.0), bins=200, density=True, color=\"red\")\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "show(30)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "show(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Basically, the same can be applied to discrete variables as well: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from pymoo.core.population import Population\n",
    "from pymoo.core.problem import Problem\n",
    "from pymoo.operators.mutation.pm import PolynomialMutation\n",
    "from pymoo.operators.repair.rounding import RoundingRepair\n",
    "\n",
    "\n",
    "def show(eta_mut):\n",
    "    problem = Problem(n_var=1, xl=-20, xu=20)\n",
    "\n",
    "    X = np.full((5000, 1), 0.0)\n",
    "    pop = Population.new(X=X)\n",
    "\n",
    "    mutation = PolynomialMutation(prob=1.0, eta=eta_mut, repair=RoundingRepair())\n",
    "\n",
    "    off = mutation(problem, pop)\n",
    "    Xp = off.get(\"X\")\n",
    "\n",
    "    plt.hist(Xp, range=(-20, 20), bins=40, density=True, color=\"red\")\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "show(30)\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_mutation_bitflip:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bitflip Mutation (BM)\n",
    "\n",
    "The bitlip mutation randomly flips a bit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from pymoo.core.population import Population\n",
    "from pymoo.core.problem import Problem\n",
    "from pymoo.operators.mutation.bitflip import BitflipMutation\n",
    "\n",
    "n_var, n_matings = 100, 50\n",
    "\n",
    "problem = Problem(n_var=n_var, vtype=bool)\n",
    "\n",
    "X = np.full((100, 100), False)\n",
    "pop = Population.new(X=X)\n",
    "\n",
    "mutation = BitflipMutation(prob=0.5, prob_var=0.3)\n",
    "Xp = mutation(problem, pop).get(\"X\")\n",
    "\n",
    "plt.figure(figsize=(4, 4))\n",
    "plt.imshow(X != Xp, cmap='Greys', interpolation='nearest')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### API"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. autofunction:: pymoo.factory.get_mutation\n",
    "    :noindex:\n",
    "\n",
    ".. autofunction:: pymoo.core.mutation.Mutation\n",
    "    :noindex:"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
